package com.txc.pay.config;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

/**
 * @ClassName: WxPayConfig
 * @Description: 微信支付配置类
 * @Author: TXC
 * @Date: 2023-12-25 10:27
 **/
@EqualsAndHashCode(callSuper = true)
@Data
public class WxPayConfig extends PayConfig implements Serializable {
    //merchantId
    private String merchantId;
    //商户证书序列号
    private String merchantSerialNumber;
    //商户APIv3密钥
    private String apiV3Key;
    //支付通知地址
    private String payNotifyUrl;

    @Override
    public String toString() {
        return JSONUtil.toJsonStr(this);
    }


    public static WxPayConfig jsonToBean(String json) {
        if (StrUtil.isNotEmpty(json)) {
            try {
                WxPayConfig wxPayConfig = JSONUtil.toBean(json, WxPayConfig.class);
                if (wxPayConfig.verifyParam()){
                    return wxPayConfig;
                }else {
                    throw new IllegalArgumentException("AliPayConfig verification failed: " + wxPayConfig);
                }
            }catch (Exception e) {
                throw new IllegalArgumentException("Failed to parse JSON to WxPayConfig", e);
            }

        }else {
            throw new IllegalArgumentException("支付宝支付配置信息不能为空");
        }
    }

    @Override
    public boolean verifyParam() {
        super.verifyParam();
        return this.merchantId != null
                && this.merchantSerialNumber != null && this.apiV3Key != null;
    }
}
